<?php 
defined('RUN_KATANA') or die('Restricted access');
require_once('controllers/ComAdController.php');
class ComAdvertiseController extends ObjectController
{
	public $Info;
	public $Error;
	public $JsActionRun;
	public $Forms;
	public $Editor;
	
	public function ComAdvertiseController()
	{
		self::runPreActions();
		self::runPostActions();
	}
	
	public function runPreActions()
	{
		global $adc;
		global $user;
		global $katana;
		
		$adc->setTTL(0);
		
		switch($katana->getAction())
		{	
			case 'new_ad':
				self::getForm('ad_cat')->Validate();
				self::getForm('new_ad')->Validate();
				if(self::getForm('ad_cat')->getValid() && self::getForm('new_ad')->getValid() && $user->getId() > 0)
				{
					$p1 = self::getForm('ad_cat')->getFieldsParamArray();
					$p2 = self::getForm('new_ad')->getFieldsParamArray();
					$params = array_merge($p1,$p2);
					$params['userId'] = $user->getId();
					$newAd 	= new Ad($params);
					$cadc 	= new ComAdController();
					$newAd 	= $cadc->addAd($newAd);
					if(is_object($newAd))
					{
						if($newAd->getId() > 0)
						{ 
							$adc->setCurrentAdId($newAd->getId());
							$adc->loadAds($newAd->getId());
							
							$adc->setCurrentAdTypeId($adc->getCurrentAd()->getAdTypeId());
							$adc->setCurrentAdCatId($adc->getCurrentAd()->getAdCatId());
							
							$adc->loadAdCats();
							$adc->loadAdBrands();
							
							$adc->getCurrentAd()->setIsValid(0);
							self::setEditor(COMPONENTS_PATH.'advertiser/view/ad/editor.php');
							$katana->addAnalyticsTrack("_gaq.push(['_trackPageview', '/ad/new/step2']);");
						}
					}
				}
				return;
			break;
			
			case 'new_ad_wizard':
				self::getForm('ad_cat_wizard')->Validate();
				self::getForm('new_ad_wizard')->Validate();
				if(self::getForm('ad_cat_wizard')->getValid() && self::getForm('new_ad_wizard')->getValid() && $user->getId() > 0)
				{
					$p1 = self::getForm('ad_cat_wizard')->getFieldsParamArray();
					$p2 = self::getForm('new_ad_wizard')->getFieldsParamArray();
					$params = array_merge($p1,$p2);
					$params['userId'] = $user->getId();
					$newAd 	= new Ad($params);
					$cadc 	= new ComAdController();
					$newAd 	= $cadc->addAd($newAd);
					if(is_object($newAd))
					{
						if($newAd->getId() > 0)
						{ 
							$adc->setCurrentAdId($newAd->getId());
							$adc->loadAds($newAd->getId());
							
							$adc->setCurrentAdTypeId($adc->getCurrentAd()->getAdTypeId());
							$adc->setCurrentAdCatId($adc->getCurrentAd()->getAdCatId());
							
							$adc->loadAdCats();
							$adc->loadAdBrands();
							
							$adc->getCurrentAd()->setIsValid(0);
							self::setEditor(COMPONENTS_PATH.'advertiser/view/ad/editor.php');
							$katana->addAnalyticsTrack("_gaq.push(['_trackPageview', '/ad/new/step2']);");
						}
					}
				}
				return;
			break;
			
			case 'editor_contacts':
				if($_POST['delContact'] > 0)
				{
					if(is_object($user->getUserContact(intval($_POST['delContact']))))
					{ $user->deleteUserContact(intval($_POST['delContact'])); }
				}
				else
				{
					self::getForm('new_contact')->Validate();
					if(self::getForm('new_contact')->getValid() && $user->getId() > 0)
					{
						$fields = self::getForm('new_contact')->getFields();
						$newContact = new UserContact();
						$newContact->setUserId($user->getId());
						$newContact->setName($fields['newContactName']->getValue());
						$newContact->setLastName($fields['newContactLastName']->getValue());
						$newContact->setEmail($fields['newContactEmail']->getValue());
						$newContact->setPhone($fields['newContactPhone']->getValue());
						$newContact->insert();
						
						$fields['newContactName']->setValue('');
						$fields['newContactLastName']->setValue('');
						$fields['newContactEmail']->setValue('');
						$fields['newContactPhone']->setValue('');
					}
				}
				return;
			break;
			
			case 'editor_locations':
				if($_POST['delLocation'] > 0)
				{
					if(is_object($user->getUserLocation(intval($_POST['delLocation']))))
					{ $user->deleteUserLocation(intval($_POST['delLocation'])); }
				}
				else
				{
					self::getForm('new_location')->Validate();
					if(self::getForm('new_location')->getValid() && $user->getId() > 0)
					{
						$fields = self::getForm('new_location')->getFields();
						$newLocation = new UserLocation();
						$newLocation->setUserId($user->getId());
						$newLocation->setCountry($fields['newLocationCountry']->getValue());
						$newLocation->setCity($fields['newLocationCity']->getValue());
						$newLocation->setZip($fields['newLocationZip']->getValue());
						$newLocation->setAddress($fields['newLocationAddress']->getValue());
						$newLocation->insert();
						
						$fields['newLocationCountry']->setValue('');
						$fields['newLocationCity']->setValue('');
						$fields['newLocationZip']->setValue('');
						$fields['newLocationAddress']->setValue('');
					}
				}
				return;
			break;
			
			case 'payment':
			case 'payment_callback':
			case 'payment_accept':
				if(intval($_REQUEST['orderid']) > 0)
				{ 
					global $shop;
					global $cart;
					$shop->setCurrentOrderId(intval($_REQUEST['orderid']));
					$shop->CurrentOrder = new Order();
					$shop->CurrentOrder->setId(intval($_REQUEST['orderid']));
					$shop->CurrentOrder->loadValues();
					
					if((intval($_REQUEST['transact']) > 0) || ($_REQUEST['cancel']))
					{
						if($shop->CurrentOrder->getUserID() > 0)
						{
							if(intval($_REQUEST['transact']) > 0)
							{ 
								$shop->CurrentOrder->setPaymentTransact(intval($_REQUEST['transact']));
								$shop->CurrentOrder->updateOrder();
								$katana->addAnalyticsTrack("_gaq.push(['_trackPageview', '/ad/new/payment/success']);");
								
								//Taken from complete.php
								if(file_exists(ORDERS_PATH.'order_'.$shop->CurrentOrder->getId().'_confirmation.html'))
								{ require_once(ORDERS_PATH.'order_'.$shop->CurrentOrder->getId().'_confirmation.html'); }
								else
								{ echo $shop->CurrentOrder->sendOrderConfirmation(); }
								
								if($shop->CurrentOrder->getConfirmed() == 1)
								{ $cart->clearCart($user); }
								
								$adc->setCurrentAdId($shop->CurrentOrder->getAdId());
								if(is_object($adc->getCurrentAd()))
								{
									if($adc->CurrentAd->getOrderId() == 0)
									{ 
										$adc->CurrentAd->setOrderId($shop->CurrentOrder->getId()); 
										$adc->CurrentAd->setDataChanged(true);
									}
									if($adc->CurrentAd->getDatePaid() == '0000-00-00 00:00:00')
									{ 
										$adc->CurrentAd->setDatePaid(date('y-m-d H:i:s')); 
										$adc->CurrentAd->setDataChanged(true);
									}
									$adc->CurrentAd->Update();
									$adc->CurrentAd->Publish();
								}
								//Taken from complete.php END
							}
						}
					}
				}
				return;
			break;
			
			default:
				if($katana->getEdit() == 'payment')
				{
					global $shop;
					global $cart;
					
					$katana->addAnalyticsTrack("_gaq.push(['_trackPageview', '/ad/new/payment']);");
					
					$cart->clearCart();
					$ad = $adc->getCurrentAd();
					if($ad->getProductId() > 0)
					{
						$shop->setCurrentProductId($ad->getProductId());
						$product = $shop->getCurrentProduct();
						$userdetails = new UserDetails($user);
						$orderid = 0;
						if(is_object($product))
						{
							$varId = $product->getFirstVariationId();
							if($ad->getGalleryDisplay() > 0)
							{
								$adc->CurrentAd->setViewsGallery(0);
								$var = $product->getVariation(0, 2);
								if(is_object($var))
								{ $varId = $var->getId(); }
							}
							if($cart->getCartItemsCount() < 1)
							{ 
								$cart->addItems($product->getId(), $varId, 1);
								if($ad->getAdQuotas() > 0)
								{ $cart->addItems(7, 17, $ad->getAdQuotas()); } //Ad quotas productID:7
							}
						}
						if($cart->getCartItemsCount() > 0)
						{
							$shipping = new Shipping($userdetails, $cart);
							$method['shipping'] = $shipping->getMethod(8);
						}
						if($method['shipping']['id'] > 0)
						{
							$payment = new Payment();
							$method['payment'] = $payment->getMethod(2);
						}
						if(count($method) > 1)
						{
							$shop->CurrentOrder = new Order();
							$shop->CurrentOrder->setConfirmed(0);
							$shop->CurrentOrder->setVals($method['shipping'], $method['payment'], 'NOW()');
							$shop->CurrentOrder->setAdId($adc->getCurrentAdId());
						}
						if(is_object($shop->CurrentOrder))
						{
							$shop->CurrentOrder->loadValues($userdetails);
							$shop->CurrentOrder->clearOrderItems();
							$shop->CurrentOrder->registerOrder();
							if($shop->CurrentOrder->getId() > 0)
							{
								$shop->setCurrentOrderId($shop->CurrentOrder->getId());
								$orderid = $shop->CurrentOrder->getId();
							}
							else
							{
								$orderid = $shop->CurrentOrder->getId();
								$shop->setCurrentOrderId($orderid);
							}
							if($shop->CurrentOrder->getConfirmed() == 1)
							{
								$adc->CurrentAd->setOrderId($shop->getCurrentOrderId());
								$adc->CurrentAd->setDataChanged(true);
								$adc->CurrentAd->Update();
							}
						}
					}
				}
			break;
		}
		
		switch($katana->getList())
		{
			case 'ad':
				if(intval($_GET['activate']) > 0)
				{
					$adc->setCurrentAdId(intval($_GET['activate']));
					if(is_object($adc->getCurrentAd()))
					{
						$ad = $adc->getCurrentAd();
						$ad->Publish();
					}
				}
				
				if(intval($_GET['archive']) > 0)
				{
					$adc->setCurrentAdId(intval($_GET['archive']));
					if(is_object($adc->getCurrentAd()))
					{
						$ad = $adc->getCurrentAd();
						$ad->archive();
					}
				}
				
				if(intval($_GET['unarchive']) > 0)
				{
					$adc->setCurrentAdId(intval($_GET['unarchive']));
					if(is_object($adc->getCurrentAd()))
					{
						$ad = $adc->getCurrentAd();
						$ad->unArchive();
					}
				}
				
				if(intval($_GET['delete']) > 0)
				{
					$adc->setCurrentAdId(intval($_GET['delete']));
					if(is_object($adc->getCurrentAd()))
					{
						$ad = $adc->getCurrentAd();
						$ad->delete();
					}
				}
				
				if(intval($_POST['del_ad_id']) > 0)
				{
					$adc->setCurrentAdId(intval($_POST['del_ad_id']));
					if(is_object($adc->getCurrentAd()))
					{
						$ad = $adc->getCurrentAd();
						$ad->delete();
					}
				}
				
				if(intval($_GET['complete']) > 0)
				{
					$adc->setCurrentAdId(intval($_GET['complete']));
					if(is_object($adc->getCurrentAd()))
					{
						$ad = $adc->getCurrentAd();
						if(($ad->getIsPaid() > 0) && ($ad->getActive() > 0) && ($ad->getUserId() == $user->getId()))
						{
							$ad->setIsCompleted(1);
							$ad->setDataChanged(1);
							$ad->updateAd();
							$katana->addAnalyticsTrack("_gaq.push(['_trackPageview', '/ad/complete/success']);");
						}
					}
				}
				
				if(intval($_GET['uncomplete']) > 0)
				{
					$adc->setCurrentAdId(intval($_GET['uncomplete']));
					if(is_object($adc->getCurrentAd()))
					{
						$ad = $adc->getCurrentAd();
						if(($ad->getIsPaid() > 0) && ($ad->getActive() > 0) && ($ad->getUserId() == $user->getId()))
						{
							$ad->setIsCompleted(0);
							$ad->setDataChanged(1);
							$ad->updateAd();
						}
					}
				}
			break;
			
			case 'search':
				if(intval($_GET['activate']) > 0)
				{
					$adc->setCurrentAdSearchId(intval($_GET['activate']));
					if(is_object($adc->getCurrentAdSearch()))
					{
						$adc->getCurrentAdSearch()->setActive(1);
						$adc->getCurrentAdSearch()->setDataChanged(1);
						$adc->getCurrentAdSearch()->update();
					}
				}
				if(intval($_GET['deactivate']) > 0)
				{
					$adc->setCurrentAdSearchId(intval($_GET['deactivate']));
					if(is_object($adc->getCurrentAdSearch()))
					{
						$adc->getCurrentAdSearch()->setActive(0);
						$adc->getCurrentAdSearch()->setDataChanged(1);
						$adc->getCurrentAdSearch()->update();
					}
				}
				if(intval($_GET['delete']) > 0)
				{
					$adc->setCurrentAdSearchId(intval($_GET['delete']));
					if(is_object($adc->getCurrentAdSearch()))
					{
						if($adc->deleteAdSearch($adc->getCurrentAdSearch()->getId()))
						{ 
							unset($adc->CurrentAdSearch); 
							unset($adc->AdSearch[intval($_GET['delete'])]); 
						}
					}
				}
			break;
			
			case 'bookmark':
				if(intval($_GET['delete']) > 0)
				{
					if(is_object($adc->getAdBookmark(intval($_GET['delete']))))
					{
						if($adc->deleteAdBookmark(intval($_GET['delete'])))
						{ unset($adc->AdBookmark[intval($_GET['delete'])]); }
					}
				}
			break;
		}
		
		
		self::loadEditor();
	}
	
	public function runPostActions()
	{
		global $adc;
		global $katana;
		global $user;
		switch ($katana->getAction())
		{
			case 'renew':
				if(intval($_POST['renew_ad_id']) > 0)
				{
					self::getForm('ad_renew')->Validate();
					if(is_object($adc->getCurrentAd()))
					{
						if($adc->CurrentAd->getUserId() == $user->getId())
						{
							$adc->CurrentAd->setGalleryDisplay(intval($_POST['GalleryDisplay']));
							$adc->CurrentAd->setUseCredits(intval($_POST['UseCredits']));
							
							$cadc 	= new ComAdController();
							$adc->CurrentAd->setProductId($cadc->findAdProductId($adc->CurrentAd));
		
							if(($adc->CurrentAd->getProductId() > 0) || ($adc->CurrentAd->getOrderId() > 0) || ($adc->CurrentAd->getGalleryDisplay() > 0))
							{
								if(($adc->CurrentAd->getUseCredits() > 0) && ($user->getCreditsLeft() > 0) && ($adc->CurrentAd->getGalleryDisplay() == 0))
								{
									$adc->CurrentAd->setAdQuotas(-1);
									$adc->CurrentAd->setProductId(0);
									
									$creditsUsed = 0;
									$creditsUsed = ($user->getCreditsUsed() + 1);
									$user->setCreditsUsed($creditsUsed);
									$user->saveUser();
								}
								
								$adc->CurrentAd->setOrderId(0);
								$adc->CurrentAd->setDatePaid('0000-00-00 00:00:00');
								$sql  = 'UPDATE '.DB_PERFIX.'shop_orders SET ';
								$sql .= '`AdId`="0"';
								$sql .= ' WHERE AdId='.$adc->CurrentAd->getId().';';
								mysql_query($sql);
							}
							
							$adc->CurrentAd->setDateCreated(date('Y-m-d H:i:s'));
							$adc->CurrentAd->setDateActiveStart('0000-00-00 00:00:00');
							$adc->CurrentAd->setDateActiveEnd('0000-00-00 00:00:00');
							//$adc->CurrentAd->setDateModified(date('Y-m-d H:i:s'));
							$adc->CurrentAd->setActive(0);
							$adc->CurrentAd->setArchived(0);
							$adc->CurrentAd->setIsCompleted(0);
							$adc->CurrentAd->setViewsGallery(0);
							$adc->CurrentAd->setIsValid(1); //Valid if previously valid
							$adc->CurrentAd->setDataChanged(1);
							$adc->CurrentAd->updateAd();
							
							global $cart;
							$cart->clearCart();
							
							$katana->addAnalyticsTrack("_gaq.push(['_trackPageview', '/ad/renew/success']);");
						}
					}
				}
			break;
			case 'editor_ad':
				if($_GET['view'] != 'images')
				{
					self::getForm('editor_ad')->Validate();
					self::getForm('editor_ad_cat')->Validate();
					self::getForm('editor_ad_price')->Validate();
					self::getForm('editor_ad_contact')->Validate();
					self::getForm('editor_ad_location')->Validate();
					
					if(($adc->getSetting('UserLevelAdDatePublish') > 0) && ($user->getPartnerStatus() >= $adc->getSetting('UserLevelAdDatePublish')))
					{ self::getForm('editor_ad_publish')->Validate(); }
					if(is_object(self::getForm('editor_form_ad_type_cat')))
					{ self::getForm('editor_form_ad_type_cat')->Validate(); }
					
					$fields0 = array();
					$fields1 = array();
					$fields2 = array();
					$fields3 = array();
					$fields4 = array();
					$fields5 = array();
					$fields6 = array(); 
					$isValid = 1;
					
					if(self::getForm('editor_ad')->getValid())
					{ $fields0 = self::getForm('editor_ad')->getFields(); }
					else
					{ $isValid = 0; }
					
					if(self::getForm('editor_ad_cat')->getValid())
					{ $fields1 = self::getForm('editor_ad_cat')->getFields(); }
					else
					{ $isValid = 0; }
					
					if(self::getForm('editor_ad_price')->getValid())
					{ $fields2 = self::getForm('editor_ad_price')->getFields(); }
					else
					{ 
						if($adc->getCurrentAd()->getUsePrice() > 0)
						{ $isValid = 0; }
					}
					
					if(self::getForm('editor_ad_contact')->getValid())
					{ $fields3 = self::getForm('editor_ad_contact')->getFields(); }
					else
					{ $isValid = 0; }
					
					if(self::getForm('editor_ad_location')->getValid())
					{ $fields4 = self::getForm('editor_ad_location')->getFields(); }
					else
					{ $isValid = 0; }
					
					if(is_object(self::getForm('editor_form_ad_type_cat')))
					{
						if(self::getForm('editor_form_ad_type_cat')->getValid())
						{ $fields5 = self::getForm('editor_form_ad_type_cat')->getFields(); }
						else
						{ $isValid = 0; }
					}
					
					if(($adc->getSetting('UserLevelAdDatePublish') > 0) && ($user->getPartnerStatus() >= $adc->getSetting('UserLevelAdDatePublish')))
					{
						if(is_object(self::getForm('editor_ad_publish')))
						{
							if(self::getForm('editor_ad_publish')->getValid())
							{ $fields6 = self::getForm('editor_ad_publish')->getFields(); }
							else
							{ $isValid = 0; }
						}
					}
					if((count($fields0) > 0) ||
					   (count($fields1) > 0) || 
					   (count($fields2) > 0) || 
					   (count($fields3) > 0) || 
					   (count($fields4) > 0) || 
					   (count($fields5) > 0) ||
					   (count($fields6) > 0))
					{
						$fields = array_merge($fields0, $fields1, $fields2, $fields3, $fields4, $fields5, $fields6);
						$cadc = new ComAdController();
						$cadc->editAd($fields, $isValid);
					}
					self::setEditor(COMPONENTS_PATH.'advertiser/view/ad/editor.php');
					
					$katana->addAnalyticsTrack("_gaq.push(['_trackPageview', '/ad/edit/success']);");
				}
			
				if(($_FILES['Images']) || ($_FILES['Image']) || (intval($_POST['del_img_id']) > 0) || ($_POST['up'] > 0) || ($_POST['down'] > 0))
				{ 
					$cadc = new ComAdController();
					$cadc->editAdImages(); 
				}
				if(($_FILES['File']) || (intval($_POST['del_file_id']) > 0) || ($_POST['up_file'] > 0) || ($_POST['down_file'] > 0))
				{ 
					$cadc = new ComAdController();
					$cadc->editAdFiles(); 
				}
			break;
		}
		self::loadEditor();
	}
	
	public function loadEditor()
	{
		global $katana;
		global $user;
		
		if($user->getId() > 0)
		{
			if($katana->getEdit() != '')
			{
				if(is_file(COMPONENTS_PATH.'advertiser/view/'.$katana->getEdit().'/editor.php'))
				{ self::setEditor(COMPONENTS_PATH.'advertiser/view/'.$katana->getEdit().'/editor.php'); }
			}
			if($katana->getList() != '')
			{
				if(is_file(COMPONENTS_PATH.'advertiser/view/'.$katana->getList().'/list.php'))
				{ self::setEditor(COMPONENTS_PATH.'advertiser/view/'.$katana->getList().'/list.php'); }
			}
			if(self::getEditor() == '')
			{ self::setEditor(COMPONENTS_PATH.'advertiser/view/profile/editor.php'); }
		}
		else
		{ self::setEditor(COMPONENTS_PATH.'advertiser/view/profile/editor.php'); }
	}
	
	public function loadForms($key='')
	{
		global $adc;
		global $txt;
		global $user;
		switch ($key)
		{
			case 'ad_cat':
				$item = $adc->getCurrentAd();
				$this->Forms[$key] = new Form;
				$this->Forms[$key]->HumanCheck = 0;
				//$this->Forms[$key]->Fields['name'] 		= new FormField('name', 'type', 'label', show, req, nospace, 'validt', min, max);
				$this->Forms[$key]->Fields['typeId'] 	= new FormField('typeId', 'select', 'category', true, true, false, 'num', 1, 0);
				$this->Forms[$key]->Fields['typeId']->setParam('onchange', 'populateCats(\'new_TypeId\', \'new_CatId\');');
				$this->Forms[$key]->Fields['typeId']->setParam('id', 'new_TypeId');
				$this->Forms[$key]->Fields['typeId']->setParam('size', '6');
				$this->Forms[$key]->Fields['typeId']->setClass('large form-control');
				if($_POST['typeId'] > 0)
				{ $this->Forms[$key]->Fields['typeId']->setValue(intval($_POST['typeId'])); }
				else
				{ 
					if(is_object($item) && ($_POST['action'] != 'editor_ad'))
					{ $this->Forms[$key]->Fields['typeId']->setValue($item->getTypeId()); }
					else
					{ $this->Forms[$key]->Fields['typeId']->setValue(1); }
				}
				
				if($adc->getAdTypes())
				{
					foreach($adc->getAdTypes() as $type)
					{ $this->Forms[$key]->Fields['typeId']->addOption($type->getId(), $type->getTitle()); }
				}
				
				$this->Forms[$key]->Fields['catId'] 	= new FormField('catId', 'select', 'subcategory', true, true, false, 'num', 1, 0);
				$this->Forms[$key]->Fields['catId']->setParam('id', 'new_CatId');
				$this->Forms[$key]->Fields['catId']->setParam('size', '14');
				$this->Forms[$key]->Fields['catId']->setClass('large form-control');
				if($adc->getSetting('NewAdBrands') > 0)
				{ $this->Forms[$key]->Fields['catId']->setParam('onchange', 'populateBrands(\'new_TypeId\', \'new_CatId\', \'new_BrandId\');'); }
				else
				{ $this->Forms[$key]->Fields['catId']->setParam('onchange', 'populateIsBuying(\'new_TypeId\', \'new_CatId\');'); }
				
				if($_POST['catId'] > 0)
				{ $this->Forms[$key]->Fields['catId']->setValue(intval($_POST['catId'])); }
				else
				{
					if(is_object($item) && ($_POST['action'] != 'editor_ad'))
					{ $this->Forms[$key]->Fields['catId']->setValue($item->getCatId()); }
				}
				if($adc->getAdCats())
				{
					foreach($adc->getAdCats() as $cat)
					{ 
						if(($this->Forms[$key]->Fields['typeId']->getValue() == $cat->getTypeId()) && ($cat->getLinkToTypeId() == 0) && ($cat->getLinkToCatId() == 0))
						{  $this->Forms[$key]->Fields['catId']->addOption($cat->getId(), $cat->getTitle()); }
					}
				}
				
			break;
			
			case 'ad_cat_wizard':
				$item = $adc->getCurrentAd();
				$this->Forms[$key] = new Form;
				$this->Forms[$key]->HumanCheck = 0;
				$this->Forms[$key]->Fields['typeId'] 	= new FormField('typeId', 'select', '', true, true, false, 'num', 1, 0);
				$this->Forms[$key]->Fields['typeId']->setParam('id', 'new_TypeId');
				$this->Forms[$key]->Fields['typeId']->setParam('style', 'display:none;');
				if($_REQUEST['typeId'] > 0)
				{ $this->Forms[$key]->Fields['typeId']->setValue(intval($_REQUEST['typeId'])); }
				else
				{ $this->Forms[$key]->Fields['typeId']->setValue(1); }
				
				if($adc->getAdTypes())
				{
					foreach($adc->getAdTypes() as $type)
					{ $this->Forms[$key]->Fields['typeId']->addOption($type->getId(), $type->getTitle()); }
				}
				
				$this->Forms[$key]->Fields['catId'] 	= new FormField('catId', 'select', '', true, true, false, 'num', 1, 0);
				$this->Forms[$key]->Fields['catId']->setParam('id', 'new_CatId');
				$this->Forms[$key]->Fields['catId']->setParam('style', 'display:none;');

				if($_REQUEST['catId'] > 0)
				{ $this->Forms[$key]->Fields['catId']->setValue(intval($_REQUEST['catId'])); }
				
				if($adc->getAdCats())
				{
					foreach($adc->getAdCats() as $cat)
					{ 
						if($this->Forms[$key]->Fields['typeId']->getValue() == $cat->getTypeId())
						{ $this->Forms[$key]->Fields['catId']->addOption($cat->getId(), $cat->getTitle()); }
					}
				}
				
				if($adc->getSetting('NewAdBrands') > 0)
				{
					$this->Forms[$key]->Fields['brandId'] 	= new FormField('brandId', 'select', 'brand', true, true, false, 'num', 1, 0);
					$this->Forms[$key]->Fields['brandId']->setParam('id', 'new_BrandId');
					$this->Forms[$key]->Fields['brandId']->setParam('style', 'display:none;');
					//$this->Forms[$key]->Fields['brandId']->setParam('onchange', 'populateIsBuying(\'new_TypeId\', \'new_CatId\');');
					
					if($_REQUEST['brandId'] > 0)
					{ $this->Forms[$key]->Fields['brandId']->setValue(intval($_REQUEST['brandId'])); } 
					
					if($adc->getAdBrands()) 
					{
						foreach($adc->getAdBrands() as $brand)
						{ $this->Forms[$key]->Fields['brandId']->addOption($brand->getId(), $brand->getTitle()); }
					}
					$this->Forms[$key]->Fields['spacer0'] 		= new FormField('spacer0', 'spacer');
				}
			break;
			
			case 'new_ad':
				$params['class'] = "large";
				$this->Forms[$key] = new Form;
				$this->Forms[$key]->HumanCheck = 0;
				
				$this->Forms[$key]->Fields['info'] 	= new FormField('info', 'info');
				$this->Forms[$key]->Fields['info']->setInfo('<b>'.ucfirst($txt->getWord('ad_type')).'</b>');
				$this->Forms[$key]->Fields['spacer1'] 		= new FormField('spacer1', 'spacer');
				
				unset($params);
				
				$this->Forms[$key]->Fields['IsBuying1'] 	= new FormField('IsBuying1', 'radio', '', true, false, false, 'num', 0, 1);
				$this->Forms[$key]->Fields['IsBuying1']->setClass('btn-group IsBuying1 IsBuyingButtons');
				$this->Forms[$key]->Fields['IsBuying1']->setParam('style', 'display:none;');
				$this->Forms[$key]->Fields['IsBuying1']->addOption(1, ucfirst($txt->getWord('selling')), '', array('id'=>'IsBuying11', 'class'=>'btn btn-primary'));
				$this->Forms[$key]->Fields['IsBuying1']->addOption(2, ucfirst($txt->getWord('buying')), '', array('id'=>'IsBuying12', 'class'=>'btn btn-info'));
				$this->Forms[$key]->Fields['IsBuying1']->addOption(3, ucfirst($txt->getWord('for_rent')), '', array('id'=>'IsBuying13', 'class'=>'btn btn-primary'));
				$this->Forms[$key]->Fields['IsBuying1']->addOption(4, ucfirst($txt->getWord('To_rent')), '', array('id'=>'IsBuying14', 'class'=>'btn btn-info'));
				
				$this->Forms[$key]->Fields['IsBuying5'] 	= new FormField('IsBuying5', 'radio', '', true, false, false, 'num', 0, 1);
				$this->Forms[$key]->Fields['IsBuying5']->setClass('btn-group IsBuying5 IsBuyingButtons');
				$this->Forms[$key]->Fields['IsBuying5']->setParam('style', 'display:none;');
				$this->Forms[$key]->Fields['IsBuying5']->addOption(1, ucfirst($txt->getWord('offering')), '', array('id'=>'IsBuying51', 'class'=>'btn btn-primary'));
				$this->Forms[$key]->Fields['IsBuying5']->addOption(2, ucfirst($txt->getWord('Requested')), '', array('id'=>'IsBuying52', 'class'=>'btn btn-info'));
				
				$this->Forms[$key]->Fields['IsBuying6'] 	= new FormField('IsBuying6', 'radio', '', true, false, false, 'num', 0, 1);
				$this->Forms[$key]->Fields['IsBuying6']->setClass('btn-group IsBuying6 IsBuyingButtons');
				$this->Forms[$key]->Fields['IsBuying6']->setParam('style', 'display:none;');
				$this->Forms[$key]->Fields['IsBuying6']->addOption(1, ucfirst($txt->getWord('selling_6')), '', array('id'=>'IsBuying61', 'class'=>'btn btn-primary'));
				$this->Forms[$key]->Fields['IsBuying6']->addOption(2, ucfirst($txt->getWord('buying_6')), '', array('id'=>'IsBuying62', 'class'=>'btn btn-info'));

				$this->Forms[$key]->Fields['spacer2'] 		= new FormField('spacer2', 'spacer');
				
				$this->Forms[$key]->Fields['IsBuying'] 	= new FormField('IsBuying', 'hidden', '', true, true, false, 'num', 0, 1);
				$this->Forms[$key]->Fields['IsBuying']->setParam('id', 'IsBuying');
				
				$this->Forms[$key]->Fields['IsRent'] 	= new FormField('IsRent', 'hidden', '', true, true, false, 'num', 0, 1);
				$this->Forms[$key]->Fields['IsRent']->setParam('id', 'IsRent');
				
				if($_POST['IsBuying'] > 0)
				{ $this->Forms[$key]->Fields['IsBuying']->setValue(intval($_POST['IsBuying'])); }
				else
				{ $this->Forms[$key]->Fields['IsBuying']->setParam('value', '0'); }
				
				if($_POST['IsRent'] > 0)
				{ $this->Forms[$key]->Fields['IsRent']->setValue(intval($_POST['IsRent'])); }
				else
				{ $this->Forms[$key]->Fields['IsRent']->setParam('value', '0'); }
				
				if($adc->getSetting('AdGallery') > 0)
                {
					$this->Forms[$key]->Fields['GalleryDisplay'] = new FormField('GalleryDisplay', 'checkbox', '', true, false, false, 'num', 0, 1);
					$this->Forms[$key]->Fields['GalleryDisplay']->setParam('style', 'display:none;');
					$this->Forms[$key]->Fields['GalleryDisplay']->setParam('class', 'GalleryDisplay alert');
					
					$params['id'] = 'GalleryDisplay';
					$this->Forms[$key]->Fields['GalleryDisplay']->addOption(1, ucfirst($txt->getWord('ad_gallery_display').' '.str_replace('x', 10, $txt->getWord('for_x_days')).' (kr. 150,- eks. mva)<br /><hr />'.$adc->getSetting('AdGalleryTxt')), '', $params);
					
					if($adc->getSetting('AdGalleryTxt') != '')
					{
						$this->Forms[$key]->Fields['AdGalleryTxt'] 	= new FormField('AdGalleryTxt', 'info');
						$this->Forms[$key]->Fields['AdGalleryTxt']->setInfo($adc->getSetting('AdGalleryInfoTxt'));
						$this->Forms[$key]->Fields['AdGalleryTxt']->setParam('class', 'GalleryDisplay alert');
					}
				}
				
				if(($user->getPartnerStatus() < 2) && ($adc->getSetting('AdQuotas') > 0))
				{
					$this->Forms[$key]->Fields['spacer3'] 		= new FormField('spacer3', 'spacer');
					if($user->getCreditsLeft() > 0)
					{
						$this->Forms[$key]->Fields['InfoUseCredits'] 	= new FormField('InfoUseCredits', 'info');
						$info  = '<p class="bold AdQuotas" style="display:none;">'.ucfirst($txt->getWord('you_have')).' '.($user->getCreditsLeft()).' '.ucfirst($txt->getWord('credit').' '.$txt->getWord('remaining')).'.<br />';
						$info .= '1 '.ucfirst($txt->getWord('credit')).' '.$txt->getWord('will_be_substracted_on_creation').'.';
						$info .= '</p>';
						$info .= '<p class="AdQuotas" style="display:none;">';
						$info .= '('.ucfirst($txt->getWord('this_does_not_apply_to')).' '.$txt->getWord('ads').' '.$txt->getWord('with').' '.$txt->getWord('ad_gallery_display').')';
						$info .= '</p>';
						
						$this->Forms[$key]->Fields['InfoUseCredits']->setInfo($info);
						$this->Forms[$key]->Fields['UseCredits'] 	= new FormField('UseCredits', 'hidden', '', true, false, false, 'num', 0, 1);
						$this->Forms[$key]->Fields['UseCredits']->setParam('value', '1');
						$this->Forms[$key]->Fields['UseCredits']->setValue(1);
					}
					else
					{
						$this->Forms[$key]->Fields['AdQuotas'] 	= new FormField('AdQuotas', 'select', 'I_wish_also_to_pay_for_additional', true, false, false, 'numeric', 0, 10);
						$this->Forms[$key]->Fields['AdQuotas']->setClass('medium-l input-large AdQuotas');
						$this->Forms[$key]->Fields['AdQuotas']->setParam('style', 'display:none;');
						$this->Forms[$key]->Fields['AdQuotas']->setParam('id', 'AdQuotas');
						
						$this->Forms[$key]->Fields['AdQuotas']->setParamLabel('class', 'AdQuotas form-control');
						$this->Forms[$key]->Fields['AdQuotas']->setParamLabel('style', 'display:none;');
						
						$this->Forms[$key]->Fields['AdQuotas']->addOption(0, ucfirst($txt->getWord('none').' '.$txt->getWord('ads'))); 
						$this->Forms[$key]->Fields['AdQuotas']->addOption(1, '1 '.ucfirst($txt->getWord('ad')));
						for($i=2; $i < 11; $i++)
						{ $this->Forms[$key]->Fields['AdQuotas']->addOption($i, $i.' '.ucfirst($txt->getWord('ads'))); }
						
						if($_POST['AdQuotas'] > 0)
						{ $this->Forms[$key]->Fields['AdQuotas']->setValue(intval($_POST['AdQuotas'])); }
						else
						{ $this->Forms[$key]->Fields['AdQuotas']->setValue(0); }
					}
				}
				
				//#PARTNER#
				if($user->getPartnerStatus() > 1)
				{
					$adc->setUserId($user->getId());
					$adc->Search['AND'] = ' AND ad.productId=0 AND ad.isBuying=0 AND ad.isRent=0 AND ad.typeId<5';
					
					if($user->getPartnerStatus() <= $adc->getAdsActiveCount())
					{
						$this->Forms[$key]->Fields['infoUpgrade'] 	= new FormField('infoUpgrade', 'info');
						$info  = '<p class="bold">'.ucfirst($txt->getWord('you_have')).' '.$adc->getAdsActiveCount().' '.ucfirst($txt->getWord('x_active_partner_ads')).'.</p>';
						$info .= '<p>('.ucfirst($txt->getWord('included').' '.$txt->getWord('unpublished_new_ads')).')</p><br />';
						$info .= '<p>'.ucfirst($txt->getWord('your')).' '.$txt->getWord('partner_account').' '.$txt->getWord('allows').' '.$user->getPartnerStatus().' ';
						$info .= ''.$txt->getWord('x_active_partner_ads').'.</p>';
						$info .= '<p>'.ucfirst($txt->getWord('you_upgrade_partner_package_choosing')).' <b>'.ucfirst($txt->getWord('upgrade')).'</b>.</p>';
						$info .= '<p>'.ucfirst($txt->getWord('if_no_wish_upgrade_partner_package_choose')).': <b>'.ucfirst($txt->getWord('no')).'</b>.</p>';
						$info .= '<p>'.ucfirst($txt->getWord('in_that_case_you_will_pay_manually_for_this_ad')).'.</p><br />';
						$info .= '<p class="bold">'.$txt->getWord('do_you_want_to_upgrade_partner_package').'?</p>';
						
						$this->Forms[$key]->Fields['infoUpgrade']->setInfo($info);
						$this->Forms[$key]->Fields['infoUpgrade']->setClass('infoUpgrade');
						
						$this->Forms[$key]->Fields['PartnerStatusUpgrade'] 	= new FormField('PartnerStatusUpgrade', 'radio', '', true, false, false, 'num', 0, 1);
						$this->Forms[$key]->Fields['PartnerStatusUpgrade']->setSrcValDef(1);
						$this->Forms[$key]->Fields['PartnerStatusUpgrade']->setClass('Upgrade');
						$this->Forms[$key]->Fields['PartnerStatusUpgrade']->addOption(1, ucfirst($txt->getWord('upgrade')), '', array('id'=>'UpgradeYes', 'class'=>'UpgradeYes'));
						$this->Forms[$key]->Fields['PartnerStatusUpgrade']->addOption(2, ucfirst($txt->getWord('no').' '.$txt->getWord('pay_manually_for_this_ad')), '', array('id'=>'UpgradeNo', 'class'=>'UpgradeNo'));
						$this->Forms[$key]->Fields['UpgradeCheck'] 	= new FormField('UpgradeCheck', 'hidden', '', true, true, false, 'num', 0, 1);
						$this->Forms[$key]->Fields['UpgradeCheck']->setParam('id', 'UpgradeCheck');
						$this->Forms[$key]->Fields['UpgradeCheck']->setValue(1);
						
						$this->Forms[$key]->Fields['spacer4'] 		= new FormField('spacer4', 'spacer');
					}
					$adc->Search['AND'] = '';
				}
				//#PARTNER-END#
				unset($params);
			break;
			
			case 'new_ad_wizard':
				$params['class'] = "large";
				$this->Forms[$key] = new Form;
				$this->Forms[$key]->HumanCheck = 0;
				
				$this->Forms[$key]->Fields['info'] 	= new FormField('info', 'info');
				$this->Forms[$key]->Fields['info']->setInfo('<b>'.ucfirst($txt->getWord('choose').' '.$txt->getWord('ad_type')).'</b>');
				
				unset($params);
				
				$this->Forms[$key]->Fields['IsBuying1'] = new FormField('IsBuying1', 'radio', '', true, false, false, 'num', 0, 1);
				$this->Forms[$key]->Fields['IsBuying1']->setClass('btn-group IsBuying1 IsBuyingButtons');
				$this->Forms[$key]->Fields['IsBuying1']->setParam('style', 'display:none;');
				$this->Forms[$key]->Fields['IsBuying1']->addOption(1, ucfirst($txt->getWord('selling')), '', array('id'=>'IsBuying11', 'class'=>'btn btn-primary'));
				$this->Forms[$key]->Fields['IsBuying1']->addOption(2, ucfirst($txt->getWord('buying')), '', array('id'=>'IsBuying12', 'class'=>'btn btn-primary'));
				$this->Forms[$key]->Fields['IsBuying1']->addOption(3, ucfirst($txt->getWord('for_rent')), '', array('id'=>'IsBuying13', 'class'=>'btn btn-primary'));
				$this->Forms[$key]->Fields['IsBuying1']->addOption(4, ucfirst($txt->getWord('To_rent')), '', array('id'=>'IsBuying14', 'class'=>'btn btn-primary'));
				
				$this->Forms[$key]->Fields['spacer2'] 		= new FormField('spacer2', 'spacer');
				
				$this->Forms[$key]->Fields['IsBuying'] 	= new FormField('IsBuying', 'hidden', '', true, true, false, 'num', 0, 1);
				$this->Forms[$key]->Fields['IsBuying']->setParam('id', 'IsBuying');
				
				$this->Forms[$key]->Fields['IsRent'] 	= new FormField('IsRent', 'hidden', '', true, true, false, 'num', 0, 1);
				$this->Forms[$key]->Fields['IsRent']->setParam('id', 'IsRent');
				
				if($_POST['IsBuying'] > 0)
				{ $this->Forms[$key]->Fields['IsBuying']->setValue(intval($_POST['IsBuying'])); }
				else
				{ $this->Forms[$key]->Fields['IsBuying']->setParam('value', '0'); }
				
				if($_POST['IsRent'] > 0)
				{ $this->Forms[$key]->Fields['IsRent']->setValue(intval($_POST['IsRent'])); }
				else
				{ $this->Forms[$key]->Fields['IsRent']->setParam('value', '0'); }
				
				if($adc->getSetting('AdGallery') > 0)
                {
					$this->Forms[$key]->Fields['GalleryDisplay'] = new FormField('GalleryDisplay', 'checkbox', '', true, false, false, 'num', 0, 1);
					$this->Forms[$key]->Fields['GalleryDisplay']->setParam('style', 'display:none;');
					$this->Forms[$key]->Fields['GalleryDisplay']->setParam('class', 'GalleryDisplay');
					
					$params['id'] = 'GalleryDisplay';
					$this->Forms[$key]->Fields['GalleryDisplay']->addOption(1, ucfirst($txt->getWord('ad_gallery_display').' '.str_replace('x', 10, $txt->getWord('for_x_days')).' (kr. 150,- eks. mva)'), '', $params);
				}
				
				if(($user->getPartnerStatus() < 2) && ($adc->getSetting('AdQuotas') > 0))
				{
					$this->Forms[$key]->Fields['spacer3'] 		= new FormField('spacer3', 'spacer');
					if($user->getCreditsLeft() > 0)
					{
						$this->Forms[$key]->Fields['InfoUseCredits'] 	= new FormField('InfoUseCredits', 'info');
						$info  = '<p class="bold AdQuotas" style="display:none;">'.ucfirst($txt->getWord('you_have')).' '.($user->getCreditsLeft()).' '.ucfirst($txt->getWord('credit').' '.$txt->getWord('remaining')).'.<br />';
						$info .= '1 '.ucfirst($txt->getWord('credit')).' '.$txt->getWord('will_be_substracted_on_creation').'.';
						$info .= '</p>';
						$info .= '<p class="AdQuotas" style="display:none;">';
						$info .= '('.ucfirst($txt->getWord('this_does_not_apply_to')).' '.$txt->getWord('ads').' '.$txt->getWord('with').' '.$txt->getWord('ad_gallery_display').')';
						$info .= '</p>';
						
						$this->Forms[$key]->Fields['InfoUseCredits']->setInfo($info);
						$this->Forms[$key]->Fields['UseCredits'] 	= new FormField('UseCredits', 'hidden', '', true, false, false, 'num', 0, 1);
						$this->Forms[$key]->Fields['UseCredits']->setParam('value', '1');
						$this->Forms[$key]->Fields['UseCredits']->setValue(1);
					}
					else
					{
						$this->Forms[$key]->Fields['AdQuotas'] 	= new FormField('AdQuotas', 'select', 'I_wish_also_to_pay_for_additional', true, false, false, 'numeric', 0, 10);
						$this->Forms[$key]->Fields['AdQuotas']->setClass('medium-l input-large AdQuotas');
						$this->Forms[$key]->Fields['AdQuotas']->setParam('style', 'display:none;');
						$this->Forms[$key]->Fields['AdQuotas']->setParam('id', 'AdQuotas');
						
						$this->Forms[$key]->Fields['AdQuotas']->setParamLabel('class', 'AdQuotas');
						$this->Forms[$key]->Fields['AdQuotas']->setParamLabel('style', 'display:none;');
						
						$this->Forms[$key]->Fields['AdQuotas']->addOption(0, ucfirst($txt->getWord('none').' '.$txt->getWord('ads'))); 
						$this->Forms[$key]->Fields['AdQuotas']->addOption(1, '1 '.ucfirst($txt->getWord('ad')));
						for($i=2; $i < 11; $i++)
						{ $this->Forms[$key]->Fields['AdQuotas']->addOption($i, $i.' '.ucfirst($txt->getWord('ads'))); }
						
						if($_POST['AdQuotas'] > 0)
						{ $this->Forms[$key]->Fields['AdQuotas']->setValue(intval($_POST['AdQuotas'])); }
						else
						{ $this->Forms[$key]->Fields['AdQuotas']->setValue(0); }
					}
				}
				
				//#PARTNER#
				if($user->getPartnerStatus() > 1)
				{
					$adc->setUserId($user->getId());
					$adc->Search['AND'] = ' AND ad.productId=0 AND ad.isBuying=0 AND ad.isRent=0 AND ad.typeId<5';
					
					if($user->getPartnerStatus() <= $adc->getAdsActiveCount())
					{
						$this->Forms[$key]->Fields['infoUpgrade'] 	= new FormField('infoUpgrade', 'info');
						$info  = '<p class="bold">'.ucfirst($txt->getWord('you_have')).' '.$adc->getAdsActiveCount().' '.ucfirst($txt->getWord('x_active_partner_ads')).'.</p>';
						$info .= '<p>('.ucfirst($txt->getWord('included').' '.$txt->getWord('unpublished_new_ads')).')</p><br />';
						$info .= '<p>'.ucfirst($txt->getWord('your')).' '.$txt->getWord('partner_account').' '.$txt->getWord('allows').' '.$user->getPartnerStatus().' ';
						$info .= ''.$txt->getWord('x_active_partner_ads').'.</p>';
						$info .= '<p>'.ucfirst($txt->getWord('you_upgrade_partner_package_choosing')).' <b>'.ucfirst($txt->getWord('upgrade')).'</b>.</p>';
						$info .= '<p>'.ucfirst($txt->getWord('if_no_wish_upgrade_partner_package_choose')).': <b>'.ucfirst($txt->getWord('no')).'</b>.</p>';
						$info .= '<p>'.ucfirst($txt->getWord('in_that_case_you_will_pay_manually_for_this_ad')).'.</p><br />';
						$info .= '<p class="bold">'.$txt->getWord('do_you_want_to_upgrade_partner_package').'?</p>';
						
						$this->Forms[$key]->Fields['infoUpgrade']->setInfo($info);
						$this->Forms[$key]->Fields['infoUpgrade']->setClass('infoUpgrade');
						
						$this->Forms[$key]->Fields['PartnerStatusUpgrade'] 	= new FormField('PartnerStatusUpgrade', 'radio', '', true, false, false, 'num', 0, 1);
						$this->Forms[$key]->Fields['PartnerStatusUpgrade']->setSrcValDef(1);
						$this->Forms[$key]->Fields['PartnerStatusUpgrade']->setClass('Upgrade');
						$this->Forms[$key]->Fields['PartnerStatusUpgrade']->addOption(1, ucfirst($txt->getWord('upgrade')), '', array('id'=>'UpgradeYes', 'class'=>'UpgradeYes'));
						$this->Forms[$key]->Fields['PartnerStatusUpgrade']->addOption(2, ucfirst($txt->getWord('no').' '.$txt->getWord('pay_manually_for_this_ad')), '', array('id'=>'UpgradeNo', 'class'=>'UpgradeNo'));
						$this->Forms[$key]->Fields['UpgradeCheck'] 	= new FormField('UpgradeCheck', 'hidden', '', true, true, false, 'num', 0, 1);
						$this->Forms[$key]->Fields['UpgradeCheck']->setParam('id', 'UpgradeCheck');
						$this->Forms[$key]->Fields['UpgradeCheck']->setValue(1);
						
						$this->Forms[$key]->Fields['spacer4'] 		= new FormField('spacer4', 'spacer');
					}
					$adc->Search['AND'] = '';
				}
				//#PARTNER-END#
				unset($params);
			break;
			
			case 'ad_renew':
				$item = $adc->getCurrentAd();
				$cadc 	= new ComAdController();
				
				$this->Forms[$key] = new Form;
				$this->Forms[$key]->HumanCheck = 0;
				
				if(($adc->getSetting('AdGallery') > 0) && (($item->getGalleryDisplay() > 0) || ($cadc->findAdTypeProductId($adc->getCurrentAd()) > 0)))
                {
					$this->Forms[$key]->Fields['GalleryDisplay'] 	= new FormField('GalleryDisplay', 'select', ucfirst($txt->getWord('ad_gallery_display').' '.str_replace('x', 10, $txt->getWord('for_x_days')).' (kr. 150,- eks. mva)'), true, false, false, 'num', 1, 0);
					$this->Forms[$key]->Fields['GalleryDisplay']->setParam('size', '2');
					$this->Forms[$key]->Fields['GalleryDisplay']->setClass('large form-control');
					$this->Forms[$key]->Fields['GalleryDisplay']->addOption(1, ucfirst($txt->getWord('renew')).' - '.ucfirst($txt->getWord('with')).' '.$txt->getWord('ad_gallery_display'));
					$this->Forms[$key]->Fields['GalleryDisplay']->addOption(0, ucfirst($txt->getWord('renew')).' - '.ucfirst($txt->getWord('without')).' '.$txt->getWord('ad_gallery_display'));
				}
				
				if(($user->getPartnerStatus() < 2) && ($adc->getSetting('AdQuotas') > 0))
				{
					$this->Forms[$key]->Fields['spacer3'] 		= new FormField('spacer3', 'spacer');
					if($user->getCreditsLeft() > 0)
					{
						//echo '<br />$user->getCreditsLeft():'.$user->getCreditsLeft();
						$this->Forms[$key]->Fields['InfoUseCredits'] 	= new FormField('InfoUseCredits', 'info');
						$info  = '<p class="bold AdQuotas" style="display:none;">'.ucfirst($txt->getWord('you_have')).' '.($user->getCreditsLeft()).' '.ucfirst($txt->getWord('credit').' '.$txt->getWord('remaining')).'.<br />';
						$info .= '1 '.ucfirst($txt->getWord('credit')).' '.$txt->getWord('will_be_substracted_on_creation').'.';
						$info .= '</p>';
						$info .= '<p class="AdQuotas" style="display:none;">';
						$info .= '('.ucfirst($txt->getWord('this_does_not_apply_to')).' '.$txt->getWord('ads').' '.$txt->getWord('with').' '.$txt->getWord('ad_gallery_display').')';
						$info .= '</p>';
						
						$this->Forms[$key]->Fields['InfoUseCredits']->setInfo($info);
						$this->Forms[$key]->Fields['UseCredits'] 	= new FormField('UseCredits', 'hidden', '', true, false, false, 'num', 0, 1);
						$this->Forms[$key]->Fields['UseCredits']->setParam('value', '1');
						$this->Forms[$key]->Fields['UseCredits']->setValue(1);
					}
				}
				
				$this->Forms[$key]->Fields['renew_ad_id'] 	= new FormField('renew_ad_id', 'hidden', '', true, false, false, '', 1, 100);
				$this->Forms[$key]->Fields['renew_ad_id']->setValue($item->getId());
				$this->Forms[$key]->Fields['renew_ad_id']->setParam('id', 'renew_ad_id');
				
				$this->Forms[$key]->Fields['cancel'] = new FormField('cancel', 'button', array('cancel'), true, false, false, '', 0, 0);
				$this->Forms[$key]->Fields['cancel']->setParam('style', 'margin-right:6px; padding:4px 6px;');
				$this->Forms[$key]->Fields['cancel']->setParam('class', 'bold');
				$this->Forms[$key]->Fields['cancel']->setParam('onclick', 'history.go(-1);');
				
				$this->Forms[$key]->Fields['submit'] = new FormField('submit', 'submit', array('renew', '&nbsp;&gt;&gt;'), true, false, false, '', 0, 0);
				$this->Forms[$key]->Fields['submit']->setParam('style', 'margin-right:6px; padding:4px 6px;');
				$this->Forms[$key]->Fields['submit']->setParam('class', 'bold');
				
				if(is_object($item) && ($_POST['action'] != 'editor_ad'))
				{
					if(is_object($this->Forms[$key]->Fields['GalleryDisplay']))
					{ $this->Forms[$key]->Fields['GalleryDisplay']->setValue($item->getGalleryDisplay()); }
				}
			break;
			
			case 'editor_ad_cat':
				$item = $adc->getCurrentAd();
				$this->Forms[$key] = new Form;
				$this->Forms[$key]->HumanCheck = 0;
				if(is_object($item))
				{
					//$this->Forms[$key]->Fields['name'] 		= new FormField('name', 'type', 'label', show, req, nospace, 'validt', min, max);
					
					$this->Forms[$key]->Fields['CatTitle'] 	= new FormField('CatTitle', 'hidden', '', true, false, false, 'text', 0, 0);
					$this->Forms[$key]->Fields['CatTitle']->setParam('id', 'AdCatTitle');
					$this->Forms[$key]->Fields['CatTitle']->setValue($item->getCatTitle());
					
					$this->Forms[$key]->Fields['IsBuying'] 	= new FormField('IsBuying', 'select', array('type', 'ad'), true, false, false, 'num', 0, 0);
					$this->Forms[$key]->Fields['IsBuying']->setValue($item->getIsBuying());
					$this->Forms[$key]->Fields['IsBuying']->setClass('medium-l input-large form-control');
					$this->Forms[$key]->Fields['IsBuying']->setParam('disabled', 'disabled');
					
					if($item->getIsRent() > 0)
					{
						if($item->getIsBuying() == 0)
						{  $this->Forms[$key]->Fields['IsBuying']->addOption(0, ucfirst($txt->getWord('for_rent'))); }
						if($item->getIsBuying() == 1)
						{  $this->Forms[$key]->Fields['IsBuying']->addOption(0, ucfirst($txt->getWord('To_rent'))); }
					}
					else
					{
						if($item->getIsBuying() == 0)
						{ 
							if(ucfirst($txt->getWord('selling_'.$item->getTypeId(), $words)) != 'Selling_'.$item->getTypeId())
							{ $this->Forms[$key]->Fields['IsBuying']->addOption(0, ucfirst($txt->getWord('selling_'.$item->getTypeId()))); }
							else
							{ $this->Forms[$key]->Fields['IsBuying']->addOption(0, ucfirst($txt->getWord('selling'))); }
						}
						if($item->getIsBuying() == 1)
						{ 
							if(ucfirst($txt->getWord('buying_'.$item->getTypeId(), $words)) != 'Buying_'.$item->getTypeId())
							{ $this->Forms[$key]->Fields['IsBuying']->addOption(1, ucfirst($txt->getWord('buying_'.$item->getTypeId()))); }
							else
							{ $this->Forms[$key]->Fields['IsBuying']->addOption(1, ucfirst($txt->getWord('buying'))); }
						}
					}
					
					$this->Forms[$key]->Fields['info'] 	= new FormField('info', 'info');
					$this->Forms[$key]->Fields['info']->setInfo(ucfirst($txt->getWord('category')).':&nbsp;'.$adc->getAdTypeTitle($item->getTypeId()).' / '.$adc->getAdCatTitle($item->getCatId()).'<br />'.ucfirst($txt->getWord('in_order_to_change').' '.$txt->getWord('type').' '.$txt->getWord('or').' '.$txt->getWord('category').' '.$txt->getWord('you_must').' '.$txt->getWord('toinsert').' '.$txt->getWord('new_ad')).'</p>');
					
					if($item->getUseBrand() > 0)
					{
						$adc->setCurrentAdTypeId($item->getTypeId());
						$adc->setCurrentAdCatId($item->getCatId());
						
						if(count($adc->getAdBrands()) > 0)
						{
							if($item->getUseBrand() == 6)
							{ $this->Forms[$key]->Fields['BrandId'] 	= new FormField('BrandId', 'select', array('brand_6'), true, false, false, 'num', 1, 0); }
							else
							{ $this->Forms[$key]->Fields['BrandId'] 	= new FormField('BrandId', 'select', array('brand'), true, true, false, 'num', 1, 0); }
							$this->Forms[$key]->Fields['BrandId']->setParam('size', '1');
							
							if($adc->getSetting('model-manager') > 0)
							{  $this->Forms[$key]->Fields['BrandId']->setParam('onchange', 'populateModels(\'AdBrand\', \'AdModel\');'); }
							else
							{ $this->Forms[$key]->Fields['BrandId']->setParam('onchange', 'checkAdTitle();'); }
							$this->Forms[$key]->Fields['BrandId']->setParam('id', 'AdBrand');
							
							if($item->getUseModel() && ($item->getBrandId() > 0))
							{ $this->Forms[$key]->Fields['BrandId']->setClass('medium-l input-large form-control disabled'); }
							else
							{ $this->Forms[$key]->Fields['BrandId']->setClass('medium-l input-large form-control'); }
							$this->Forms[$key]->Fields['BrandId']->addOption(0, '-');
							
							if($_POST['BrandId'] > 0)
							{ $this->Forms[$key]->Fields['BrandId']->setValue(intval($_POST['BrandId'])); }
							else
							{
								if($_POST['action'] != 'editor_ad')
								{ $this->Forms[$key]->Fields['BrandId']->setValue($item->getBrandId()); }
							}
							foreach($adc->getAdBrands() as $brand)
							{ 
								if($item->getUseBrand() > 1)
								{
									if(($brand->getTypeId() == $item->getUseBrand()) && ($item->getCatId() == $brand->getCatId()))
									{ $this->Forms[$key]->Fields['BrandId']->addOption($brand->getId(), $brand->getTitle()); }
								}
								else
								{ 
									if((($brand->getTypeId() == 0) && ($brand->getCatId() == 0)) || ($brand->getTypeId() == $item->getTypeId()))
									{ $this->Forms[$key]->Fields['BrandId']->addOption($brand->getId(), $brand->getTitle()); }
								}
							}
						}
					}
					
					if($item->getUseModel())
					{
						if($adc->getSetting('model-manager') > 0)
						{
							$this->Forms[$key]->Fields['ModId'] 	= new FormField('ModId', 'select', array('model'), true, true, false, 'num', 1, 0);
							$this->Forms[$key]->Fields['ModId']->setParam('size', '1');
							$this->Forms[$key]->Fields['ModId']->setParam('onchange', 'checkAdTitle();');
							$this->Forms[$key]->Fields['ModId']->setParam('id', 'AdModel');
							$this->Forms[$key]->Fields['ModId']->setClass('medium-l input-large');
							$this->Forms[$key]->Fields['ModId']->addOption(0, '-');
							
							if($_POST['ModId'] > 0)
							{ $this->Forms[$key]->Fields['ModId']->setValue(intval($_POST['ModId'])); }
							else
							{ $this->Forms[$key]->Fields['ModId']->setValue($item->getModId()); }
							
							$adc->setCurrentAdBrandId($item->getBrandId());
							if($item->getBrandId() > 0)
							{
								if(count($adc->getAdModels()) > 0)
								{
									foreach($adc->getAdModels() as $model)
									{ $this->Forms[$key]->Fields['ModId']->addOption($model->getId(), $model->getTitle()); }
								}
							}
						}
						else
						{
							$this->Forms[$key]->Fields['Model'] 		= new FormField('Model', 'text', 'model', true, true, false, 'text', 1, 100);
							$this->Forms[$key]->Fields['Model']->setClass('medium-l input-large');
							$this->Forms[$key]->Fields['Model']->setParam('id', 'AdModel');
							$this->Forms[$key]->Fields['Model']->setParam('onkeyup', 'checkAdTitle();');
							$this->Forms[$key]->Fields['Model']->setParam('onkeydown', 'checkAdTitle();');
						}
					}
					
					if($item->getUseYear())
					{
						if($item->getIsBuying())
						{ $label = array('year_production', 'from'); }
						else
						{ $label = array('year_production'); }
						
						$this->Forms[$key]->Fields['Year'] 	= new FormField('Year', 'select', $label, true, false, false, 'numeric', 0, 0);
						$this->Forms[$key]->Fields['Year']->setParam('onchange', 'checkAdTitle();');
						$this->Forms[$key]->Fields['Year']->setClass('medium-l input-large');
						$this->Forms[$key]->Fields['Year']->setParam('id', 'AdYear');
						for($i=intval(date('Y')); $i > 1900; $i--)
						{ $this->Forms[$key]->Fields['Year']->addOption($i, $i); }
					}
					
					if(is_object($item) && ($_POST['action'] != 'editor_ad'))
					{
						if(is_object($this->Forms[$key]->Fields['Model']))
						{ $this->Forms[$key]->Fields['Model']->setValue($item->getModel()); }
						if(is_object($this->Forms[$key]->Fields['ModId']))
						{ $this->Forms[$key]->Fields['ModId']->setValue($item->getModId()); }
						if(is_object($this->Forms[$key]->Fields['Year']))
						{ $this->Forms[$key]->Fields['Year']->setValue($item->getYear()); }
					}
				}
			break;
			
			case 'editor_ad_price':
				$this->Forms[$key] = new Form;
				$this->Forms[$key]->HumanCheck = 0;
				$item = $adc->getCurrentAd();
				if(is_object($item))
				{
					global $shop;
					if($item->getIsBuying())
					{ $label = array('max', 'price'); }
					else
					{ $label = array('price'); }
					
					if($adc->getSetting('UseCurrencies') > 0)
					{
						$this->Forms[$key]->Fields['PriceLocal'] 		= new FormField('PriceLocal', 'text', $label, true, true, false, 'numeric', 0, 0);
						$this->Forms[$key]->Fields['PriceLocal']->setClass('small numeric form-control');
						
						$params['onkeyup'] = 'convertFromCurrency(\'CurrencyISO\', \'priceLocal\', \'priceGlobal\');';
						$params['onclick'] = 'this.value = \'\';';
						$params['id'] = 'priceLocal';
						$params['class'] = 'numeric bold form-control';
						$this->Forms[$key]->Fields['PriceLocal']->setParams($params);
						unset($params);
						
						$params['id'] = 'CurrencyISO';
						$params['class'] = 'bold form-control';
						$params['onchange'] = 'convertToCurrency(\'CurrencyISO\', \'priceGlobal\', \'priceLocal\');';
						$this->Forms[$key]->Fields['PriceCurrency'] 		= new FormField('PriceCurrency', 'select', 'currency', true, true, false, 'alpha', 0, 0);
						$this->Forms[$key]->Fields['PriceCurrency']->setParams($params);
						$curs = $shop->getCurrencies();
						if(count($curs) > 0)
						{
							foreach($curs as $cur)
							{ $this->Forms[$key]->Fields['PriceCurrency']->addOption($cur['code'], $cur['code']); }
						}
					}
					
					unset($params);
					unset($label);
					if($item->getIsBuying())
					{ $label = array('max', 'price', '('.$adc->getSetting('GlobalCurrency').')'); }
					else
					{ $label = array('price', '('.$adc->getSetting('GlobalCurrency').')'); }
					
					if($adc->getSetting('UseCurrencies') > 0)
					{
						$this->Forms[$key]->Fields['PriceGlobal'] 		= new FormField('PriceGlobal', 'hidden', $label, true, true, false, 'numeric', 0, 0);
						$params['onkeyup'] = 'convertToCurrency(\'CurrencyISO\', \'priceGlobal\', \'priceLocal\');';
					}
					else
					{ $this->Forms[$key]->Fields['PriceGlobal'] 		= new FormField('PriceGlobal', 'text', $label, true, true, false, 'numeric', 0, 0); }
					$this->Forms[$key]->Fields['PriceGlobal']->setClass('small numeric');
					$params['id'] = 'priceGlobal';
					$params['class'] = 'numeric bold form-control';
					$this->Forms[$key]->Fields['PriceGlobal']->setParams($params);
					
					$this->Forms[$key]->Fields['info'] 	= new FormField('info', 'info');
					$this->Forms[$key]->Fields['info']->setInfo('<b>'.ucfirst($txt->getWord('all')).'</b>');
					
					if($item->getIsBuying() == 0)
					{ 
						unset($params);
						
						$this->Forms[$key]->Fields['PriceDisplay'] 	= new FormField('PriceDisplay', 'radio', '', true, false, false, 'num', 0, 1);
						$this->Forms[$key]->Fields['PriceDisplay']->setClass('radio inline');
						
						$params['id'] = "PriceDisplay1";
						$params['class'] = "PriceDisplay";
						$params['onclick'] = 'togglePrice(1);';
						$this->Forms[$key]->Fields['PriceDisplay']->addOption(1, ucfirst($txt->getWord('display').' '.$txt->getWord('price')), '', $params);
						$params['id'] = "PriceDisplay2";
						$params['class'] = "PriceDisplay";
						$params['onclick'] = 'togglePrice(0);';
						$this->Forms[$key]->Fields['PriceDisplay']->addOption(0, ucfirst($txt->getWord('price_at_request')), '', $params);
					}
					
					if(is_object($item) && ($_POST['action'] != 'editor_ad'))
					{
						if(is_object($this->Forms[$key]->Fields['PriceCurrency']))
						{ $this->Forms[$key]->Fields['PriceCurrency']->setValue($item->getPriceCurrency()); }
						if(is_object($this->Forms[$key]->Fields['PriceLocal']))
						{ $this->Forms[$key]->Fields['PriceLocal']->setValue($item->getPriceLocal()); }
						if(is_object($this->Forms[$key]->Fields['PriceGlobal']))
						{ $this->Forms[$key]->Fields['PriceGlobal']->setValue($item->getPriceGlobal()); }
						if(is_object($this->Forms[$key]->Fields['PriceDisplay']))
						{ $this->Forms[$key]->Fields['PriceDisplay']->setValue($item->getPriceDisplay()); }
					}
				}
			break;
			
			case 'editor_ad':
				$item = $adc->getCurrentAd();
				$this->Forms[$key] = new Form;
				
				$params['class'] = "large";
				$this->Forms[$key]->HumanCheck = 0;
				
				if($adc->getSetting('UserAdTitle') > 0)
				{
					$this->Forms[$key]->Fields['Title'] 		= new FormField('Title', 'text', array('title'), true, true, false, 'text', 1, 100);
					$this->Forms[$key]->Fields['Title']->setClass('medium-l input-large');
					$this->Forms[$key]->Fields['Title']->setParam('id', 'AdTitle');
				}
				else
				{
					$this->Forms[$key]->Fields['Title'] 		= new FormField('Title', 'hidden', '', true, false, false, 'text', 1, 100);
					$this->Forms[$key]->Fields['Title']->setClass('medium-l input-large');
					$this->Forms[$key]->Fields['Title']->setParam('id', 'AdTitle');
				}
				
				$this->Forms[$key]->Fields['info'] 	= new FormField('info', 'info');
				if(SITE_ID == 23)
				{ $this->Forms[$key]->Fields['info']->setInfo('<span>Annonseoverskrift kan endres etter eget &oslash;nske.</span>'); }
				$this->Forms[$key]->Fields['spacer1'] 		= new FormField('spacer1', 'spacer');
				
				if($adc->getSetting('UseEnglish'))
				{
					$this->Forms[$key]->Fields['TitleEn'] 		= new FormField('TitleEn', 'text', array('optional', 'ad', 'title', '(', 'english', ')'), true, false, false, 'text', 0, 100);
					$this->Forms[$key]->Fields['TitleEn']->setClass('medium-l input-large');
					$this->Forms[$key]->Fields['TitleEn']->setParam('id', 'AdTitleEn');
				}
				
				$this->Forms[$key]->Fields['Description'] 	= new FormField('Description', 'textarea', array('description'), true, false, false, 'text', 0, 3000);
				$this->Forms[$key]->Fields['Description']->setClass('tinymce_mini medium-l input-large limit_3000');
				$this->Forms[$key]->Fields['Description']->setParam('rows', '8');
				$this->Forms[$key]->Fields['Description']->setParam('id', 'AdDescription');
				
				if(($adc->getSetting('UserLevelAdUrl') > 0) && ($user->getPartnerStatus() >= $adc->getSetting('UserLevelAdUrl')))
				{
					$this->Forms[$key]->Fields['ProductLinkTxt'] = new FormField('ProductLinkTxt', 'text', array('link_title'), true, false, false, 'text', 1, 100);
					$this->Forms[$key]->Fields['ProductLinkTxt']->setClass('medium-l input-large');
					
					$this->Forms[$key]->Fields['ProductLink'] = new FormField('ProductLink', 'text', array('product_link'), true, false, false, 'url', 1, 400);
					$this->Forms[$key]->Fields['ProductLink']->setClass('medium-l input-large');
					
					$this->Forms[$key]->Fields['ProductLinkTxt2'] = new FormField('ProductLinkTxt2', 'text', array('link_title', '2'), true, false, false, 'text', 1, 100);
					$this->Forms[$key]->Fields['ProductLinkTxt2']->setClass('medium-l input-large');
					
					$this->Forms[$key]->Fields['ProductLink2'] = new FormField('ProductLink2', 'text', array('product_link', '2'), true, false, false, 'url', 1, 400);
					$this->Forms[$key]->Fields['ProductLink2']->setClass('medium-l input-large');
				}
				
				if($adc->getSetting('UseEnglish'))
				{
					$this->Forms[$key]->Fields['DescriptionEn'] 	= new FormField('DescriptionEn', 'textarea', array('optional', 'description', '(', 'english', ')'), true, false, false, 'text', 0, 3000);
					$this->Forms[$key]->Fields['DescriptionEn']->setClass('medium-l input-large limit_3000');
					$this->Forms[$key]->Fields['DescriptionEn']->setParam('rows', '4');
					$this->Forms[$key]->Fields['DescriptionEn']->setParam('id', 'AdDescriptionEn');
				}
				
				
				
				$this->Forms[$key]->Fields['spacer1'] 		= new FormField('spacer1', 'spacer');
				
				unset($params);
				
				if($item->getUseIsNew())
				{
					$this->Forms[$key]->Fields['IsNew'] 	= new FormField('IsNew', 'radio', '', true, false, false, 'num', 0, 1);
					//$this->Forms[$key]->Fields['IsNew']->setClass('jqUiRadio');
					$this->Forms[$key]->Fields['IsNew']->setClass('radio inline');
					
					$params['id'] = "IsNew1";
					$this->Forms[$key]->Fields['IsNew']->addOption(0, ucfirst($txt->getWord('used')), '', $params);
					$params['id'] = "IsNew2";
					$this->Forms[$key]->Fields['IsNew']->addOption(1, ucfirst($txt->getWord('new')), '', $params);
				}
				
				if(is_object($item) && ($_POST['action'] != 'editor_ad'))
				{
					$this->Forms[$key]->Fields['Title']->setValue($item->getTitle());
					$this->Forms[$key]->Fields['Description']->setValue($item->getDescription());
					if($adc->getSetting('UseEnglish'))
					{
						$this->Forms[$key]->Fields['TitleEn']->setValue($item->getTitleEn());
						$this->Forms[$key]->Fields['DescriptionEn']->setValue($item->getDescriptionEn());
					}
					if(is_object($this->Forms[$key]->Fields['IsNew']))
					{ $this->Forms[$key]->Fields['IsNew']->setValue($item->getIsNew()); }
					if(($adc->getSetting('UserLevelAdUrl') > 0) && ($user->getPartnerStatus() >= $adc->getSetting('UserLevelAdUrl')))
					{ 
						$this->Forms[$key]->Fields['ProductLink']->setValue($item->getProductLink()); 
						if($this->Forms[$key]->Fields['ProductLink2'])
						{ $this->Forms[$key]->Fields['ProductLink2']->setValue($item->getProductLink2()); }
						if($this->Forms[$key]->Fields['ProductLinkTxt'])
						{ $this->Forms[$key]->Fields['ProductLinkTxt']->setValue($item->getProductLinkTxt());  }
						if($this->Forms[$key]->Fields['ProductLinkTxt2'])
						{ $this->Forms[$key]->Fields['ProductLinkTxt2']->setValue($item->getProductLinkTxt2()); }
					}
				}
			break;
			
			case 'editor_ad_publish':
				$item = $adc->getCurrentAd();
				$this->Forms[$key] = new Form;
				if(($adc->getSetting('UserLevelAdDatePublish') > 0) && ($user->getPartnerStatus() >= $adc->getSetting('UserLevelAdDatePublish')))
				{
					$params['class'] = "large";
					$this->Forms[$key]->HumanCheck = 0;
					
					$this->Forms[$key]->Fields['info'] 	= new FormField('info', 'info');
					$this->Forms[$key]->Fields['info']->setInfo('<span>Publiseringsdato</span>');
					$this->Forms[$key]->Fields['spacer1'] 		= new FormField('spacer1', 'spacer');
					
					if($user->getPartnerStatus() >= $adc->getSetting('UserLevelAdDatePublish'))
					{
						$this->Forms[$key]->Fields['DateActiveStart'] = new FormField('DateActiveStart', 'text', array('DateActiveStart'), true, false, false, 'date', 0, 10);
						$this->Forms[$key]->Fields['DateActiveStart']->setClass('small date_pick_future');
						
						$this->Forms[$key]->Fields['TimeActiveStart'] = new FormField('TimeActiveStart', 'select', array('TimeActiveStart'), true, false, false, 'text', 0, 8);
						$this->Forms[$key]->Fields['TimeActiveStart']->setClass('medium input-small pull-left');
						for($i=0; $i < 24; $i++)
						{ 
							if($i < 10)
							{ $this->Forms[$key]->Fields['TimeActiveStart']->addOption('0'.$i.':00:00', 'kl. 0'.$i.':00'); }
							else
							{ $this->Forms[$key]->Fields['TimeActiveStart']->addOption($i.':00:00', 'kl. '.$i.':00'); }
						}
					}
					
					if(is_object($item) && ($_POST['action'] != 'editor_ad'))
					{ 
						$this->Forms[$key]->Fields['DateActiveStart']->setValue(substr($item->getDateActiveStart(),0,10));
						$this->Forms[$key]->Fields['TimeActiveStart']->setValue(substr($item->getDateActiveStart(),11,8));
					}
				}
			break;
			
			case 'editor_ad_contact':
				$userdetails = new UserDetails($user);
				
				$item = $adc->getCurrentAd();
				$this->Forms[$key] = new Form;
				
				$params['class'] = "large";
				$this->Forms[$key]->HumanCheck = 0;
				
				//$this->Forms[$key]->Fields['name'] 		= new FormField('name', 'type', 'label', show, req, nospace, 'validt', min, max, $comp='', $val='', $params=Array());
				
				if($user->getUserContacts())
				{
					$this->Forms[$key]->Fields['ContactId'] 	= new FormField('ContactId', 'select', 'contact_persons', true, false, false, 'num');
					$this->Forms[$key]->Fields['ContactId']->setClass('small-m form-control');
					$this->Forms[$key]->Fields['ContactId']->setParam('id', 'ContactId');
					$this->Forms[$key]->Fields['ContactId']->setParam('onchange', 'setAdContact();');
					
					$this->Forms[$key]->Fields['ContactId']->addOption(0, ucfirst($txt->getWord('current')));
					foreach($user->getUserContacts() as $contact)
					{ $this->Forms[$key]->Fields['ContactId']->addOption($contact->getId(), $contact); }
				}
				
				$this->Forms[$key]->Fields['ContactEmail'] 		= new FormField('ContactEmail', 'email', 'e-mail', true, true, true, 'email', 4, 100);
				$this->Forms[$key]->Fields['ContactEmail']->setClass('small-m form-control');
				$this->Forms[$key]->Fields['ContactEmail']->setParam('id', 'ContactEmail');
				
				$this->Forms[$key]->Fields['ContactName'] 		= new FormField('ContactName', 'text', 'name', true, true, false, 'text', 2, 50);
				$this->Forms[$key]->Fields['ContactName']->setClass('small-m form-control');
				$this->Forms[$key]->Fields['ContactName']->setParam('id', 'ContactName');
				
				$this->Forms[$key]->Fields['ContactCompany'] 	= new FormField('ContactCompany', 'text', 'company', true, false, false, 'text', 0, 100);
				$this->Forms[$key]->Fields['ContactCompany']->setClass('small-m form-control');
				$this->Forms[$key]->Fields['ContactCompany']->setParam('id', 'ContactCompany');
				
				$this->Forms[$key]->Fields['ContactMob'] 	= new FormField('ContactMob', 'text', 'mobile', true, false, false, 'anum', 0, 40);
				$this->Forms[$key]->Fields['ContactMob']->setClass('small-m form-control');
				$this->Forms[$key]->Fields['ContactMob']->setParam('id', 'ContactMob');
				
				$this->Forms[$key]->Fields['ContactPhone'] 	= new FormField('ContactPhone', 'text', 'telephone', true, true, false, 'anum', 0, 40);
				$this->Forms[$key]->Fields['ContactPhone']->setClass('small-m form-control');
				$this->Forms[$key]->Fields['ContactPhone']->setParam('id', 'ContactPhone');
				
				if($user->getPartnerStatus() > 1)
				{
					$this->Forms[$key]->Fields['ContactWeb'] 	= new FormField('ContactWeb', 'text', 'homepage', true, false, false, 'text', 0, 200);
					$this->Forms[$key]->Fields['ContactWeb']->setClass('small-m form-control');
					$this->Forms[$key]->Fields['ContactWeb']->setParam('id', 'ContactWeb');
				}
				if(is_object($item) && ($_POST['action'] != 'editor_ad'))
				{
					if($item->getContactEmail() != '')
					{ $this->Forms[$key]->Fields['ContactEmail']->setValue($item->getContactEmail()); }
					else
					{ $this->Forms[$key]->Fields['ContactEmail']->setValue($user->getEmail()); }
					
					if($item->getContactName() != '')
					{ $this->Forms[$key]->Fields['ContactName']->setValue($item->getContactName()); }
					else
					{ $this->Forms[$key]->Fields['ContactName']->setValue($userdetails->getName().' '.$userdetails->getLastname()); }
					
					if($item->getContactCompany() != '')
					{ $this->Forms[$key]->Fields['ContactCompany']->setValue($item->getContactCompany()); }
					else
					{ $this->Forms[$key]->Fields['ContactCompany']->setValue($userdetails->getCompany()); }
				
					if($item->getContactMob() != '')
					{ $this->Forms[$key]->Fields['ContactMob']->setValue($item->getContactMob()); }
					else
					{ $this->Forms[$key]->Fields['ContactMob']->setValue($userdetails->getMob()); }
					
					if($item->getContactPhone() != '')
					{ $this->Forms[$key]->Fields['ContactPhone']->setValue($item->getContactPhone()); }
					else
					{ $this->Forms[$key]->Fields['ContactPhone']->setValue($userdetails->getPhone()); }
					
					if($user->getPartnerStatus() > 1)
					{
						if($item->getContactWeb() != '')
						{ $this->Forms[$key]->Fields['ContactWeb']->setValue($item->getContactWeb()); }
						else
						{ $this->Forms[$key]->Fields['ContactWeb']->setValue($userdetails->getWww()); }
					}
				}
			break;
			
			case 'editor_ad_location':
				$userdetails = new UserDetails($user);
				
				$item = $adc->getCurrentAd();
				$this->Forms[$key] = new Form;
				
				$params['class'] = "large";
				$this->Forms[$key]->HumanCheck = 0;
				
				//$this->Forms[$key]->Fields['name'] 		= new FormField('name', 'type', 'label', show, req, nospace, 'validt', min, max, $comp='', $val='', $params=Array());
				
				if($user->getUserLocations())
				{
					$this->Forms[$key]->Fields['LocationId'] 	= new FormField('ContactId', 'select', 'sale_locations', true, false, false, 'num');
					$this->Forms[$key]->Fields['LocationId']->setClass('small-m form-control');
					$this->Forms[$key]->Fields['LocationId']->setParam('id', 'LocationId');
					$this->Forms[$key]->Fields['LocationId']->setParam('onchange', 'setAdLocation();');
					
					$this->Forms[$key]->Fields['LocationId']->addOption(0, ucfirst($txt->getWord('current')));
					foreach($user->getUserLocations() as $location)
					{ $this->Forms[$key]->Fields['LocationId']->addOption($location->getId(), $location); }
				}
				
				$this->Forms[$key]->Fields['LocationCountry'] 	= new FormField('LocationCountry', 'select', 'country', true, true, false, 'alpha', 1, 100);
				$this->Forms[$key]->Fields['LocationCountry']->setClass('small-m form-control');
				foreach(self::getCountryList() as $country)
				{
					if($country['active'] > 0)
					{ 
						if($country['printable_name_'.LANG] != '')
						{ $this->Forms[$key]->Fields['LocationCountry']->addOption($country['iso'], $country['printable_name_'.LANG]); }
						else
						{ $this->Forms[$key]->Fields['LocationCountry']->addOption($country['iso'], $country['printable_name']); }
					}
				}

				$this->Forms[$key]->Fields['LocationCountry']->setSrcValDef(COUNTRY);
				$this->Forms[$key]->Fields['LocationCountry']->setParam('id', 'LocationCountry');
				
				$this->Forms[$key]->Fields['LocationCity'] 		= new FormField('LocationCity', 'text', 'city', true, true, false, 'text', 2, 100);
				$this->Forms[$key]->Fields['LocationCity']->setClass('small-m form-control');
				$this->Forms[$key]->Fields['LocationCity']->setParam('id', 'LocationCity');
				
				$this->Forms[$key]->Fields['LocationAddress'] 	= new FormField('LocationAddress', 'text', 'address', true, false, false, 'text', 2, 100);
				$this->Forms[$key]->Fields['LocationAddress']->setClass('small-m form-control');
				$this->Forms[$key]->Fields['LocationAddress']->setParam('id', 'LocationAddress');
				
				$this->Forms[$key]->Fields['LocationZip']	= new FormField('LocationZip', 'text', 'zip', true, false, true, 'num', 2, 10);
				$this->Forms[$key]->Fields['LocationZip']->setClass('small-m form-control');
				$this->Forms[$key]->Fields['LocationZip']->setParam('id', 'LocationZip');
				
				if(is_object($item) && ($_POST['action'] != 'editor_ad'))
				{
					if($item->getLocationAddress() != '')
					{ $this->Forms[$key]->Fields['LocationAddress']->setValue($item->getLocationAddress()); }
					else
					{ $this->Forms[$key]->Fields['LocationAddress']->setValue($userdetails->getAddress()); }
					
					if($item->getLocationZip() != '')
					{ $this->Forms[$key]->Fields['LocationZip']->setValue($item->getLocationZip()); }
					else
					{ $this->Forms[$key]->Fields['LocationZip']->setValue($userdetails->getZip(4)); }
					
					if($item->getLocationCity() != '')
					{ $this->Forms[$key]->Fields['LocationCity']->setValue($item->getLocationCity()); }
					else
					{ $this->Forms[$key]->Fields['LocationCity']->setValue($userdetails->getCity()); }
					
					if($item->getLocationCountry() != '')
					{ $this->Forms[$key]->Fields['LocationCountry']->setValue($item->getLocationCountry()); }
					else
					{ $this->Forms[$key]->Fields['LocationCountry']->setValue($userdetails->getCountry()); }
				}
			break;
			
			case 'editor_form_ad_type_cat':
				$item = $adc->getCurrentAd();
				$adType = $adc->getAdType($item->getTypeId());
				$adCat 	= $adc->getAdCat($item->getCatId());
				$this->Forms[$key] = new Form;
				
				if(is_object($adType))
				{
					if($item->getIsBuying() > 0)
					{
						if($adType->getFormBuyId() > 0)
						{ $this->Forms[$key]->setId($adType->getFormBuyId()); }
					}
					else
					{
						if($adType->getFormId() > 0)
						{ $this->Forms[$key]->setId($adType->getFormId()); }
					}
				}
				
				if(is_object($adCat))
				{
					if($item->getIsBuying() > 0)
					{
						if($adCat->getFormBuyId() > 0)
						{ $this->Forms[$key]->setId($adCat->getFormBuyId()); }
					}
					else
					{
						if($adCat->getFormId() > 0)
						{ $this->Forms[$key]->setId($adCat->getFormId()); }
					}
				}
				
				if($this->Forms[$key]->getId() > 0)
				{ 
					$this->Forms[$key]->loadFields();
					if(is_object($item) && ($_POST['action'] != 'editor_ad'))
					{
						if(count($this->Forms[$key]->getFields()) > 0)
						{
							foreach($this->Forms[$key]->getFields() as $field)
							{
								$objectVar = $field->getName();
								if($adc->varExsists($objectVar, $item))
								{
									switch($objectVar)
									{
										case 'FromDate':
										case 'ToDate':
										case 'DateStart':
										case 'DateDue':
											if(self::formatDateTime($item->__call('get'.$objectVar), 'd.m.Y') != date('d.m.Y', 0))
											{ $field->setValue($item->__call('get'.$objectVar)); }
										break;
										default:
											if(($item->__call('get'.$objectVar) == 0) && ($field->getValidType() == 'numeric') && ($field->getReq() == 0))
											{ $field->setValue(''); }
											else
											{ $field->setValue($item->__call('get'.$objectVar)); }
										break;
									}
								}
							}
						}
					}
				}
			break;
			//FormField($name='', $ty='', $lbl='', $show=true, $req=false, $nospace=false, $validt='anum', $mnl=0, $mxl=0, $comp='', $val='') 
			
			case 'new_contact':
				$this->Forms[$key] = new Form;
				$this->Forms[$key]->Fields['newContactName'] 		= new FormField('newContactName', 'text', 'name', true, true, false, 'text', 2, 50);
				$this->Forms[$key]->Fields['newContactName']->setClass('small');
				$this->Forms[$key]->Fields['newContactLastName']	= new FormField('newContactLastName', 'text', 'name', true, false, false, 'text', 2, 50);
				$this->Forms[$key]->Fields['newContactLastName']->setClass('xsmall');
				$this->Forms[$key]->Fields['newContactEmail'] 		= new FormField('newContactEmail', 'text', 'e-mail', true, false, true, 'email', 4, 100);
				$this->Forms[$key]->Fields['newContactEmail']->setClass('small');
				$this->Forms[$key]->Fields['newContactPhone'] 		= new FormField('newContactPhone', 'text', 'telephone', true, false, false, 'anum', 0, 20);
				$this->Forms[$key]->Fields['newContactPhone']->setClass('xsmall');
			break;
			
			case 'new_location':
				$this->Forms[$key] = new Form;
				$this->Forms[$key]->Fields['newLocationCountry'] 	= new FormField('newLocationCountry', 'select', 'country', true, true, false, 'alpha', 1, 100);
				$this->Forms[$key]->Fields['newLocationCountry']->setClass('medium-l input-large');
				$this->Forms[$key]->Fields['newLocationCountry']->setSrcTable('country');
				$this->Forms[$key]->Fields['newLocationCountry']->setSrcValSelect('iso');
				$this->Forms[$key]->Fields['newLocationCountry']->setSrcLblSelect('printable_name');
				//$this->Forms[$key]->Fields['newLocationCountry']->setSrcValDef(COUNTRY);
				$this->Forms[$key]->Fields['newLocationCountry']->setClass('small');
				
				$this->Forms[$key]->Fields['newLocationCity'] 		= new FormField('newLocationCity', 'text', 'city', true, true, false, 'text', 2, 100);
				$this->Forms[$key]->Fields['newLocationCity']->setClass('xsmall');
				$this->Forms[$key]->Fields['newLocationZip']		= new FormField('newLocationZip', 'text', 'zip', true, true, true, 'num', 2, 10);
				$this->Forms[$key]->Fields['newLocationZip']->setClass('xsmall');
				$this->Forms[$key]->Fields['newLocationAddress'] 	= new FormField('newLocationAddress', 'text', 'address', true, true, false, 'text', 2, 100);
				$this->Forms[$key]->Fields['newLocationAddress']->setClass('small');
			break;
		}
	}
}
$controller = new ComAdvertiseController();
?>